package com.sojson.project.sys.dict.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.sys.dict.controller.param.verify.DictDataParamVerify;
import com.sojson.project.sys.dict.entity.dto.DictDataDto;
import com.sojson.project.sys.dict.entity.dto.DictDataPageDto;
import com.sojson.project.sys.dict.entity.po.DictData;
import com.sojson.project.sys.dict.entity.vo.DictDataVo;
import com.sojson.project.sys.dict.service.DictDataService;
import com.sojson.result.Result;
import com.sojson.util.DictUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 字典类型描述表控制器
 * 
 * @author liu
 * @date 2022-09-07
 */
@Api(tags = "字典类型描述表管理")
@RestController
@RequestMapping("/sys/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/save')")
    @ApiOperation("给字典类型描述表添加一条数据")
    public Result<Boolean> save(@RequestBody DictData dto) throws IOException {
        // 校验参数
        Result<Boolean> paramResult = DictDataParamVerify.save(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.saveInit();
        return Result.success(dictDataService.add(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/updateById')")
    @ApiOperation("通过id更新字典类型描述表数据")
    public Result<Boolean> updateById(@RequestBody DictData dto) throws IOException {
        // 校验参数
        Result<Boolean> paramResult = DictDataParamVerify.updateById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(dictDataService.update(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/page')")
    @ApiOperation(value = "分页查询字典类型描述表数据", response = DictDataVo.class)
    public Result<List<DictDataVo>> page(DictDataPageDto dto) {
        return Result.success(dictDataService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/export')")
    @ApiOperation("导出字典描述表数据")
    public void export(DictDataDto dto) throws Exception {
        List<DictDataVo> list = dictDataService.list(dto);
        ExcelUtil<DictDataVo> excelUtil = new ExcelUtil<>(DictDataVo.class);
        excelUtil.exportExcelOutFile(list, "字典参数");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/info/{id}')")
    @ApiOperation(value = "通过id查询字典类型描述表数据", response = DictDataVo.class)
    public Result<DictDataVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(dictDataService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/sys/dict/data/remove')")
    @ApiOperation("通过id列表删除字典类型描述表数据")
    public Result<Boolean> remove(@RequestBody DictDataDto dto) throws IOException {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(dictDataService.removeByIds(ids));
    }

    /**
     * 通过key查询
     * 
     * @param key
     * @return
     * @throws IOException 
     */
    @GetMapping("/findByKey/{key}")
    @ApiOperation(value = "通过key查询字典类型描述表数据", response = DictDataVo.class)
    public Result<List<DictDataVo>> findValByKey(@PathVariable String key) throws IOException {
        if (key == null) {
            return Result.success();
        }

        List<DictDataVo> list = DictUtil.gets(key);
        if (StringUtil.isNotBlank(list)) {
            return Result.success(list);
        }

        return Result.success(dictDataService.findByKey(key));
    }

}